<?php
if(isset($AppUI->id) && $AppUI->id > 0 ) {
	redirect(isset($_SESSION[SESSION_BACKURL]) ? $_SESSION[SESSION_BACKURL] : HOST_BACKEND);
}
$strUsername = $this->input->post('username');
$strPassword = $this->input->post('password');
$intError	 = 0;	
if(isset($strUsername) && isset($strPassword)) {	
	if(isset($_SESSION['LOGIN_ERROR'])) unset($_SESSION['LOGIN_ERROR']);
	$this->load->model('admin_model', 'admin');	
    $this->load->model('permission_model', 'permission');
	$arrInfo 	= $this->admin->getInfoByUsername($strUsername);
	if($arrInfo) {
		$strPassDb 		= $arrInfo->password;
		$strChapassword	= md5($strPassDb);							
		if($strChapassword == $strPassword) {
			$this->session->set_userdata('admin', $arrInfo);
            // all permission all cms
            $arrPermission = $this->permission->getPermissionByAdmin($arrInfo->id);
            if(isset($arrPermission )) {
                // assign each item into session
                foreach($arrPermission as $row)  {
                    if($row['perm'] > -1) {
                        $_SESSION[$row['code']] = $row['perm'];    
                    }                    
                }              
            }
			redirect(isset($_SESSION[SESSION_BACKURL]) ? $_SESSION[SESSION_BACKURL] : HOST_BACKEND);
		} else {
			$intError = 3;
		}
	} else {
		$intError = 2;
	}
} else {
	$intError = 4;
}
if($intError > 0) {		
	$_SESSION['LOGIN_ERROR'] = array('username'=>$strUsername,'error'=>$intError);		
}
redirect(HOST_BACKEND);
